package com.wnhz.work.jy.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.HomefragBean;

import java.util.List;


/**
 * Created by Administrator on 2016/8/29 0029.
 */
public class HomeFrah1Adapter extends RecyclerView.Adapter<HomeFrah1Adapter.ViewHolder> {
    private LayoutInflater mInflater;
    private Context context;
    private List<HomefragBean> homeList;
    private int screenWidth;

    /**
     * ItemClick的回调接口
     */
    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener OnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener OnItemClickLitener) {
        this.OnItemClickLitener = OnItemClickLitener;
    }

    public HomeFrah1Adapter(Context context, List<HomefragBean> homeList, int screenWidth) {
        mInflater = LayoutInflater.from(context);
        this.context = context;
        this.homeList = homeList;
        this.screenWidth = screenWidth;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.homefrag1_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        int width = screenWidth  / 2;

        LayoutParams params = viewHolder.mImg.getLayoutParams();

        params.width = width;
        params.height = width;

        viewHolder.mImg.setLayoutParams(params);
//        ImageLoader.getInstance().displayImage(homeList.get(position).getImg(),viewHolder.mImg);
        viewHolder.mImg.setImageResource(Integer.parseInt(homeList.get(position).getImg()));
        viewHolder.name.setText(homeList.get(position).getTitle());
        viewHolder.price.setText("¥"+homeList.get(position).getPrice());

        // 如果设置了回调，则设置点击事件
        if (OnItemClickLitener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    OnItemClickLitener.onItemClick(viewHolder.itemView,position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return homeList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View arg0) {
            super(arg0);
            mImg = (ImageView) arg0.findViewById(R.id.item_home_recycle1_image);
            name = (TextView) arg0.findViewById(R.id.item_home_recycle1_name);
            price = (TextView) arg0.findViewById(R.id.item_home_recycle1_price);
        }

        View layout;
        ImageView mImg;
        TextView name;
        TextView price;
    }

}
